package com.scau.service.impl;

import com.scau.common.exception.BusinessException;
import com.scau.dao.TeacherDao;
import com.scau.entity.Counselor;
import com.scau.entity.Teacher;
import com.scau.service.TeacherService;
import com.scau.util.BeanUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class TeacherServiceImpl implements TeacherService {

    @Autowired
    private TeacherDao teacherDao;

    @Override
    public void add(Teacher teacher) {
        Teacher cache = teacherDao.save(teacher);
        BeanUtils.copyProperties(cache, teacher);
    }

    @Override
    public Teacher findOne(Long teacherId) {
        Teacher teacher = teacherDao.findOne(teacherId);

        if (teacher == null) {
            throw new BusinessException("不存在该教师");
        }
        return teacher;
    }

    @Override
    public Page<Teacher> findAll(Teacher teacher, Pageable pageable) {
        return teacherDao.findAll(Example.of(teacher), pageable);
    }

    @Transactional
    @Override
    public Teacher update(Teacher teacher) {
        Teacher existing = this.findOne(teacher.getId());
        BeanUtil.copyNonNullProperties(teacher, existing);
        teacherDao.save(existing);
        return existing;
    }

    @Override
    public void delete(Long teacherId) {
        teacherDao.delete(teacherId);
    }
}
